package com.example.hospitalmanagement.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.R.color.white
import com.example.hospitalmanagement.bean.Data
import com.example.hospitalmanagement.bean.SecondaryLinkageItem

/**
 * 预约挂号中左边列表适配器
 */
class DepartmentLeftRecyclerViewAdapter(context: Context) :
    RecyclerView.Adapter<DepartmentLeftRecyclerViewAdapter.LeftHolder>() {
    private val context = context
    lateinit var dataList: MutableList<Data>
    private lateinit var listener: (position: Int) -> Unit
    fun setListener(listener: (position: Int) -> Unit) {
        this.listener = listener
    }

    class LeftHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val leftItemIcon: ImageView = itemView.findViewById(R.id.department_item_left_view)
        val leftItemText: TextView = itemView.findViewById(R.id.department_item_left_text)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LeftHolder {
        val leftHolder = LeftHolder(
            LayoutInflater.from(context).inflate(R.layout.department_items_left, parent, false)
        )
        leftHolder.itemView.setOnClickListener {
            listener(leftHolder.adapterPosition)
        }
        return leftHolder
    }

    @SuppressLint("ResourceAsColor")
    override fun onBindViewHolder(holder: LeftHolder, position: Int) {
        holder.leftItemText.text = dataList[position].depSysName
        if (dataList[position].checked) {
            holder.leftItemIcon.isVisible = true
            holder.leftItemText.textSize = 24.0F
            holder.leftItemText.paint.isFakeBoldText = true
            holder.leftItemText.setTextColor(context.getColor(R.color.dialog_button_color))
            holder.itemView.setBackgroundColor(context.getColor(white))
        } else {
            holder.leftItemIcon.isGone = true
            holder.leftItemText.textSize = 18.0F
            holder.leftItemText.paint.isFakeBoldText = false
            holder.leftItemText.setTextColor(context.getColor(R.color.text_color))
            holder.itemView.setBackgroundColor(context.getColor(R.color.background_color))
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}